# this script checks the free space on drive Y: (which is a shared directory on 
# Ma-fs, and hence is effectively the same as the free space on Ma-fs) every 
# $frequency minutes. It accomplishes this using schtasks - if the task already 
# exists, it will simply exit after checking. If the tasks doesn't exist, it 
# creates it. If there is less than $spacethreshold space free, it calls blat to
# email $jcb_email_to to let them know. Ace. It also limits emails toevery 2
# hours if it's consistently failing.

email_to="jcb91@cam.ac.uk"
taskname="file-server-space-checker"
spacethreshold="15Gb"
bashdiskrecord="$( cd "$( dirname "$0" )" && pwd )/bashdisk.txt"
frequency=20
consistentfailfreq=$((3600*2))

# read in the free disk space (in bytes). Note the ^s for the comma & pipe.
freespace=`freedisk /d Y: $spacethreshold`
success=`echo $freespace | grep -c SUCCESS`
if [ "$success" = "1" ]
then
    echo "yeah man, there's still more than $spacethreshold free!"
    oldline=`grep -o "bashdisk last sent an email to .* at .* on .*" $bashdiskrecord`
    echo "the last bashdisk check at $(date "+%H:%M on %A %d %b %Y") passed." > $bashdiskrecord
    echo $oldline >> $bashdiskrecord
    echo "At the last check, there was more than $spacethreshold free."  >> $bashdiskrecord
else
    echo "the last bashdisk check at $(date "+%H:%M on %A %d %b %Y") failed." > $bashdiskrecord
    # decide whether to email
    emailtime=`grep -o "[0-9]* \[A-Z]\w\w [0-9]*" $bashdiskrecord`
    emaildate=`grep -o "[0-9/]*" $bashdiskrecord`
    emailseconds=$(($(date +%s)-$(date +%s -d "$emailtime $emaildate")))
    prevsuccess=`grep -c passed $bashdiskrecord`
    lastspace=`grep -o "was [0-9.]*[bkKmMgGtT][bB ]`
    if [ "$prevsuccess" = "1" -o "$lastspace" != "was $freespace"] #$emailseconds -gt $consistentfailfreq ]
    then
        echo "bashdisk last sent an email to $email_to at $(date "+%H:%M on %A %d %b %Y")" >> $bashdiskrecord
        freespace=`freedisk /d Y: | grep -o -E [0-9,]+`
        scale=$((${#freespace}/4))
        sizestr="b kbMbGbTb"
        sizestr=${sizestr:2*scale:2}
        freespace=`awk -v freespace=$freespace -v scale=$scale 'BEGIN{gsub(",","",freespace); printf "%3.1f", (freespace / (1024^scale))}'`
        echo "At the last check, there was $freespace free." >> $bashdiskrecord
        email_from=masimulation1@gmail.com
        email_pw=beaming2009
        blatopts="-server 127.0.0.1 -to $email_to -f $email_from -u $email_from -pw $email_pw"
        body="Filespace on server Ma-fs is running low! Only $freespace$sizestr remain! This is probably Josh's fault, forgetting to clear out old simulation files, so this automated email should make him do something about it :S"
        subject="Low filespace on Ma-fs!"
        blat -subject "$subject" -body "$body" $blatopts
    fi
fi

running=`schtasks /Query /nh | grep -c $taskname`
if [ "$running" -gt "0" ]
then
    echo "The scheduled task is already running:"
    schtasks
else
    hr=`date +%H`
    mn=`date +%M`
    # round minute up to nearest $frequency
    mn=$(((mn/frequency+1)*frequency))
    #calculate new hour if need be
    hr=$(((hr*60+mn)/60))
    runtime=`printf "%02d:%02d\n" $((hr%24)) $((mn%60))`
    echo "Starting scheduled task to run every $frequency minutes from $runtime."
    echo "This will require a password:"
    schtasks /Create /tn $taskname /tr "C:\cygwin\bin\bash.exe Z:\josh-phd-code\bash\bashdisk.sh" /sc MINUTE /mo $frequency /st $runtime
fi